﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Collections.Generic;
using System.Data.SqlClient;

public partial class back_Banners : System.Web.UI.Page
{
    #region Variable Declaration Block
    IDataRepository _dataObject = new DataRepository();
    Banner sBanner;
    List<Banner> sBanners; 
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillContents();
        }
    }
    private void fillContents()
    {
        sBanners = new List<Banner>();
        sBanner = new Banner(_dataObject);
        sBanners = sBanner.GetAllBanners();

        if (sBanners.Count > 0)
        {
            grdMenus.Visible = true;
            grdMenus.DataSource = sBanners;
            grdMenus.DataBind();
        }
        else
        {
            grdMenus.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdMenus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label hdnMenuId = (Label)e.Row.FindControl("hdnMenuId");
            Button btnEdit = (Button)e.Row.FindControl("btnEdit");
            btnEdit.PostBackUrl = "AddBanner.aspx?id=" + hdnMenuId.Text;
        }
    }
    protected void grdMenus_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grdMenus_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower() == "editmenu")
        {
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            Button btnEdit = (Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }

        if (e.CommandName.ToString().ToLower().Trim().Equals("deletemenu"))
        {
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            Label hdnMenuId = (Label)row.FindControl("hdnMenuId");
            sBanner = new Banner(_dataObject);
            sBanner.Id = Convert.ToInt32(hdnMenuId.Text);
            sBanner.Delete();

            fillContents();

            divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
}
